/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var App = require('app');
var c;

describe('App.ManageConfigGroupsController', function() {
  var controller = App.ManageConfigGroupsController.create({});

  beforeEach(function() {
    c = App.ManageConfigGroupsController.create({});
  });

	var manageConfigGroupsController = App.ManageConfigGroupsController.create({});

	describe('#addConfigGroup', function() {
		beforeEach(function() {
			manageConfigGroupsController.addConfigGroup();
		});

		describe("#validate", function() {
			it("should display no warning if user inputs valid characters into group name", function() {

				manageConfigGroupsController.addGroupPopup.set('configGroupName', 'hello');

				expect(manageConfigGroupsController.addGroupPopup.warningMessage).to.be.empty;
			});

			it("should display warning if user inputs invalid characters into group name", function() {
				manageConfigGroupsController.addGroupPopup.set('configGroupName', '/{"!@#$%');

				expect(manageConfigGroupsController.addGroupPopup.warningMessage).to.equal('Invalid Group Name. Only alphanumerics, hyphens, spaces and underscores are allowed.');
			});
		});
	});

	describe('#renameConfigGroup', function() {
		beforeEach(function() {
			var configGroup = Ember.Object.create ({
				name: 'name',
				description: 'description'
			});

			manageConfigGroupsController.set('selectedConfigGroup', configGroup);
			manageConfigGroupsController.renameConfigGroup();
		});

		describe("#validate", function() {
			it("should display no warning if user inputs valid characters into group name", function() {
				manageConfigGroupsController.renameGroupPopup.set('configGroupName', 'hello');

				expect(manageConfigGroupsController.renameGroupPopup.warningMessage).to.be.empty;
			});

			it("should display warning if user inputs invalid characters into group name", function() {
				manageConfigGroupsController.renameGroupPopup.set('configGroupName', '/{"!@#$%');

				expect(manageConfigGroupsController.renameGroupPopup.warningMessage).to.equal('Invalid Group Name. Only alphanumerics, hyphens, spaces and underscores are allowed.');
			});
		});
	});

  describe('#addHostsCallback', function() {

    beforeEach(function() {

      c.reopen({
        selectedConfigGroup: Em.Object.create({
          hosts: ['h1'],
          parentConfigGroup: Em.Object.create({
            hosts: ['h2', 'h3']
          })
        })
      });
    });

    it('should set hosts to selectedConfigGroup and remove them form default group', function () {

      c.addHostsCallback(['h2', 'h3']);

      expect(c.get('selectedConfigGroup.hosts')).to.include.members(['h1','h2','h3']);
      expect(c.get('selectedConfigGroup.parentConfigGroup.hosts').toArray()).to.be.empty;
    });

  });

  describe('#isHostsModified', function () {

    Em.A([
        {
          o: {
            toClearHosts: [],
            toDelete: [],
            toSetHosts: [],
            toCreate: []
          },
          e: false
        },
        {
          o: {
            toClearHosts: [{}],
            toDelete: [],
            toSetHosts: [],
            toCreate: []
          },
          e: true
        },
        {
          o: {
            toClearHosts: [],
            toDelete: [{}],
            toSetHosts: [],
            toCreate: []
          },
          e: true
        },
        {
          o: {
            toClearHosts: [],
            toDelete: [],
            toSetHosts: [{}],
            toCreate: []
          },
          e: true
        },
        {
          o: {
            toClearHosts: [],
            toDelete: [],
            toSetHosts: [],
            toCreate: [{}]
          },
          e: true
        }
    ]).forEach(function (test, index) {
        it('test #' + index, function () {
          c.reopen({
            isLoaded: true,
            hostsModifiedConfigGroups: test.o
          });
          expect(c.get('isHostsModified')).to.equal(test.e);
        });
      });

  });

  describe('#deleteConfigGroup', function () {

    beforeEach(function() {

      var defaultGroup = Em.Object.create({
        hosts: ['h2', 'h3'],
        isDefault: true
      });

      var selectedGroup = Em.Object.create({
        hosts: ['h1'],
        parentConfigGroup: defaultGroup
      });

      c.reopen({
        configGroups: [defaultGroup, selectedGroup],
        selectedConfigGroup: selectedGroup
      });
    });

    it('after deleting some config group, Default should be selected', function () {

      c.deleteConfigGroup();

      expect(c.get('selectedConfigGroup.hosts')).to.include.members(['h1','h2','h3']);
      expect(c.get('selectedConfigGroup.isDefault')).to.be.true;
    });

  });

  describe("#manageConfigurationGroups", function () {
    var service = Em.Object.create({});
    manageConfigGroupsController.set('hostsModifiedConfigGroups', {});
    describe("#controller passed", function () {
      var controller = Em.Object.create({
        content: Em.Object.create()
      });
      var popup = manageConfigGroupsController.manageConfigurationGroups(controller, service);

      describe("#onPrimary()", function () {
        beforeEach(function () {
          sinon.stub(popup, 'onPrimaryWizard', Em.K);
        });
        afterEach(function () {
          popup.onPrimaryWizard.restore();
        });
        it("", function () {
          popup.onPrimary();
          expect(popup.onPrimaryWizard.calledOnce).to.be.true;
        });
      });

      describe("#onPrimaryWizard()", function () {
        var ctrl = Em.Object.create({
          selectedService: Em.Object.create({
            selected: false
          }),
          selectedServiceObserver: Em.K,
          setGroupsToDelete: Em.K
        });
        beforeEach(function () {
          sinon.spy(ctrl, 'selectedServiceObserver');
          sinon.spy(ctrl, 'setGroupsToDelete');
          sinon.stub(manageConfigGroupsController, 'persistConfigGroups', Em.K);
          sinon.stub(popup, 'updateConfigGroupOnServicePage', Em.K);
          sinon.stub(popup, 'hide', Em.K);
        });
        afterEach(function () {
          ctrl.setGroupsToDelete.restore();
          ctrl.selectedServiceObserver.restore();
          manageConfigGroupsController.persistConfigGroups.restore();
          popup.updateConfigGroupOnServicePage.restore();
          popup.hide.restore();
        });
        it("groups deleted on 7th step", function () {
          ctrl.set('name', 'wizardStep7Controller');
          popup.onPrimaryWizard(ctrl, {toDelete: [1]});
          expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
          expect(ctrl.setGroupsToDelete.calledWith([1])).to.be.true;
          expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
          expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
          expect(popup.hide.calledOnce).to.be.true;
        });
        it("wizard not on 7th step", function () {
          ctrl.set('name', '');
          popup.onPrimaryWizard(ctrl, {});
          expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
          expect(ctrl.setGroupsToDelete.called).to.be.false;
          expect(manageConfigGroupsController.persistConfigGroups.called).to.be.false;
          expect(popup.updateConfigGroupOnServicePage.called).to.be.false;
          expect(popup.hide.calledOnce).to.be.true;
        });
        it("wizard on 7th step, service selected", function () {
          ctrl.set('name', 'wizardStep7Controller');
          ctrl.set('selectedService.selected', true);
          popup.onPrimaryWizard(ctrl, {toDelete: [1]});
          expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
          expect(ctrl.setGroupsToDelete.called).to.be.false;
          expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
          expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
          expect(popup.hide.calledOnce).to.be.true;
        });
        it("wizard on 7th step, no groups to delete", function () {
          ctrl.set('name', 'wizardStep7Controller');
          ctrl.set('selectedService.selected', false);
          popup.onPrimaryWizard(ctrl, {toDelete: []});
          expect(ctrl.selectedServiceObserver.calledOnce).to.be.true;
          expect(ctrl.setGroupsToDelete.called).to.be.false;
          expect(manageConfigGroupsController.persistConfigGroups.calledOnce).to.be.true;
          expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
          expect(popup.hide.calledOnce).to.be.true;
        });
      });
    });

    describe("#controller not passed", function () {
      var popup = manageConfigGroupsController.manageConfigurationGroups(null, service);

      describe("#onPrimary()", function () {
        beforeEach(function () {
          sinon.stub(popup, 'runClearCGQueue').returns({
            done: function (callback) {
              callback();
            }
          });
          sinon.stub(popup, 'runModifyCGQueue').returns({
            done: function (callback) {
              callback();
            }
          });
          sinon.stub(popup, 'runCreateCGQueue').returns({
            done: function (callback) {
              callback();
            }
          });
          sinon.stub(popup, 'updateConfigGroupOnServicePage', Em.K);
          sinon.stub(popup, 'hide', Em.K);
        });
        afterEach(function () {
          popup.runCreateCGQueue.restore();
          popup.runModifyCGQueue.restore();
          popup.runClearCGQueue.restore();
          popup.updateConfigGroupOnServicePage.restore();
          popup.hide.restore();
        });
        it("", function () {
          popup.onPrimary();
          expect(popup.runClearCGQueue.calledOnce).to.be.true;
          expect(popup.runModifyCGQueue.calledOnce).to.be.true;
          expect(popup.runCreateCGQueue.calledOnce).to.be.true;
          expect(popup.updateConfigGroupOnServicePage.calledOnce).to.be.true;
          expect(popup.hide.calledOnce).to.be.true;
        });
      });
      describe("#runClearCGQueue()", function () {
        beforeEach(function () {
          sinon.stub(manageConfigGroupsController, 'clearConfigurationGroupHosts', Em.K);
          sinon.stub(manageConfigGroupsController, 'deleteConfigurationGroup', Em.K);
        });
        afterEach(function () {
          manageConfigGroupsController.clearConfigurationGroupHosts.restore();
          manageConfigGroupsController.deleteConfigurationGroup.restore();
        });
        it("", function () {
          popup.runClearCGQueue(Em.K, {
            initialGroups: [],
            toClearHosts: [Em.Object.create()],
            toDelete: [1]
          });
          expect(manageConfigGroupsController.clearConfigurationGroupHosts.calledOnce).to.be.true;
          expect(manageConfigGroupsController.deleteConfigurationGroup.calledOnce).to.be.true;
        });
      });
      describe("#runModifyCGQueue()", function () {
        beforeEach(function () {
          sinon.stub(manageConfigGroupsController, 'updateConfigurationGroup', Em.K);
        });
        afterEach(function () {
          manageConfigGroupsController.updateConfigurationGroup.restore();
        });
        it("", function () {
          popup.runModifyCGQueue(Em.K, {toSetHosts: [1]});
          expect(manageConfigGroupsController.updateConfigurationGroup.calledOnce).to.be.true;
        });
      });
      describe("#runCreateCGQueue()", function () {
        beforeEach(function () {
          sinon.stub(manageConfigGroupsController, 'postNewConfigurationGroup', Em.K);
        });
        afterEach(function () {
          manageConfigGroupsController.postNewConfigurationGroup.restore();
        });
        it("", function () {
          popup.runCreateCGQueue(Em.K, {toCreate: [1]});
          expect(manageConfigGroupsController.postNewConfigurationGroup.calledOnce).to.be.true;
        });
      });
    });
  });
});
